<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="民警姓名" prop="policeName">
        <el-input
          v-model="queryParams.policeName"
          placeholder="请输入民警姓名"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="民警警号" prop="badgeNumber">
        <el-input
          v-model="queryParams.badgeNumber"
          placeholder="请输入民警警号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价时间" prop="appraiseTime">
        <el-date-picker
          v-model="queryParams.appraiseTime"
          type="date"
          placeholder="请选择评价时间"
          clearable
          format="YYYY年MM月DD日"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="所属社区" prop="communityId">
        <el-select
          v-model="queryParams.communityId"
          placeholder="请选择"
          clearable
          :empty-values="[null]"
          :value-on-clear="null"
          @change="handleQuery"
          style="width: 220px"
        >
          <el-option
            v-for="dict in community_options"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="handleStatus">
        <el-select
          v-model="queryParams.handleStatus"
          placeholder="请选择"
          clearable
          :empty-values="[null]"
          :value-on-clear="null"
          @clear="
            queryParams.handleStatus = '';
            handleQuery();
          "
          style="width: 220px"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="未处理" :value="0"></el-option>
          <el-option label="已办结" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="scoreStar">
        <el-radio-group v-model="queryParams.scoreStar">
          <el-radio value="">全部</el-radio>
          <el-radio :value="3">满意</el-radio>
          <el-radio :value="2">一般</el-radio>
          <el-radio :value="1">不满意</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['crowdInfo:serviceEvaluation:crowdEvaluation:export']"
          >下载</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="tableList" border>
      <el-table-column label="序号" type="index" align="center" width="70" />
      <el-table-column label="评价人" prop="appraiseUserName" align="center" />
      <el-table-column label="手机号" prop="phonenumber" align="center" />
      <el-table-column label="事项" prop="itemName" align="center" />
      <el-table-column
        label="内容"
        prop="content"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column label="图片" align="center">
        <template #default="scope">
          <el-image
            v-if="scope.row.imgUrl.length"
            style="width: 60px; height: 60px; margin-bottom: -5px"
            :src="scope.row.imgUrl[0]"
            :preview-src-list="scope.row.imgUrl"
          >
          </el-image>
          <span v-if="scope.row.imgUrl.length > 1">…</span>
        </template>
      </el-table-column>
      <el-table-column label="评价民警" align="center" prop="policeName" />
      <el-table-column label="警号" align="center" prop="badgeNumber" />
      <el-table-column label="所属社区" align="center" prop="communityName" />
      <el-table-column label="满意度" align="center">
        <template #default="scope">
          <div
            class="flex"
            style="
              justify-content: center;
              align-items: center;
              text-align: left;
            "
          >
            <span style="margin-right: 5px">{{
              scope.row.scoreStar === 1
                ? "不满意"
                : scope.row.scoreStar === 2
                ? "一般"
                : scope.row.scoreStar === 3
                ? "满意"
                : ""
            }}</span>
            <div v-for="(item, index) in scope.row.scoreStar" :key="index">
              <el-icon color="#f7ba2a" :size="18" style="margin-top: 5px">
                <StarFilled />
              </el-icon>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评价时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <span v-if="scope.row.handleStatus == 1" style="color: #67c23a"
            >已办结</span
          >
          <span v-if="scope.row.handleStatus == 0">未处理</span>
          <!-- <span v-if="scope.row.handleStatus == 2">未设置</span> -->
        </template>
      </el-table-column>
      <el-table-column label="状态处理" align="center">
        <template #default="scope">
          <el-button
            type="primary"
            @click="handleUpdateStatus(scope.row, 1)"
            v-hasPermi="['crowdInfo:reportAndComplaint:crowdComplaint:status']"
            >办结</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="CrowdEvaluation">
import { listEvaluation } from "@/api/crowdInfo/evaluation";
import { updateHandleStatus } from "@/api/crowdInfo/consultation";
import { getCommunity } from "@/api/community/community";

const { proxy } = getCurrentInstance();
const baseUrl = import.meta.env.VITE_APP_BASE_API;

const tableList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const community_options = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    appraiseTime: undefined,
    scoreStar: "",
    communityId: "",
    policeName: undefined,
    badgeNumber: undefined,
    handleStatus: "",
  },
});

const { queryParams } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listEvaluation(queryParams.value).then((response) => {
    const { data } = response;
    tableList.value = data.records;
    total.value = data.total;
    tableList.value = tableList.value.map((item) => {
      if (item.imgUrl) {
        item.imgUrl = item.imgUrl.split(",");
        if (item.imgUrl.length) {
          item.imgUrl = item.imgUrl.map((img) => {
            img = baseUrl + img;
            return img;
          });
        }
      } else {
        item.imgUrl = [];
      }
      return item;
    });
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 查询社区下拉列表数据 */
function getCommunityList() {
  loading.value = true;
  getCommunity({}).then((response) => {
    const { data } = response;
    community_options.value = data;
    community_options.value.unshift({
      id: "",
      name: "全部",
    });
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "appraise/exportAppraiseExcel",
    {
      ...queryParams.value,
    },
    `群众评价统计表_${new Date().getTime()}.xlsx`
  );
}

/** 办理状态改变 */
function handleUpdateStatus(row, status) {
  if (row.handleStatus == 0) {
    updateHandleStatus({
      handleStatus: status,
      id: row.id,
    }).then((response) => {
      proxy.$modal.msgSuccess("状态更改成功");
      getList();
    });
  } else {
    proxy.$modal.msgWarning("状态已变更，不可再更改！");
  }
}

getCommunityList();
getList();
</script>
